package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;

import com.atguigu.servicebase.exception.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author 涛哥
 * @since 2020-05-22
 */
@Api(description="讲师管理")
@RestController
@RequestMapping("/eduservice/edu-teacher")
public class EduTeacherController {


    @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public R findAll(){

        List<EduTeacher> list = eduTeacherService.list(null);

        return R.ok().data("items",list);
    }

    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("/delete/{id}")
    public R delete(    @ApiParam(name = "id", value = "讲师ID", required = true)
                              @PathVariable("id")String id
    ){

        boolean b = eduTeacherService.removeById(id);
            if (b){
                return R.ok();
            }else {
                return R.error();
            }

    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/pageTeacher/{current}/{limit}")
    public R pageTeacher(@PathVariable("current") Long current,
                         @PathVariable("limit") Long limit){

        Page<EduTeacher> page = new Page<>(current,limit);

        eduTeacherService.page(page,null);//将查询到的分页信息封装到了page里面

        List<EduTeacher> records = page.getRecords();//每页具体数据
        long total = page.getTotal();//总记录数



        return R.ok().data("total",total).data("rows",records);
    }


    @ApiOperation(value = "条件分页查询")
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
   public R pageTeacherCondition(@PathVariable("current") Long current,
                                 @PathVariable("limit") Long limit,
                                 @RequestBody(required = false) TeacherQuery teacherQuery){

        Page<EduTeacher> page = new Page<>(current,limit);

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();


        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);//大于等于
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);//小于等于
        }

        eduTeacherService.page(page,wrapper);

        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();

        return R.ok().data("total",total).data("rows",records);

   }

        @ApiOperation(value = "添加老师")
        @PostMapping("/addTeacher")
        public R addTeacher(@RequestBody EduTeacher eduTeacher){

            boolean save = eduTeacherService.save(eduTeacher);

            //int a = 10/0;
            if (save){
                return R.ok();
            }else {
                return R.error();
            }

        }

        @ApiOperation(value = "修改老师信息")
        @PostMapping("/updateTeacher")
        public R updaeTeacher(@RequestBody EduTeacher eduTeacher){

            boolean b = eduTeacherService.updateById(eduTeacher);
            if (b){
                return R.ok();
            }else {
                return R.error();
            }

        }

        @ApiOperation(value = "根据id查询老师")
        @GetMapping("/getTeacher/{id}")
        public R getTeacher(@ApiParam(value = "老师的id") @PathVariable("id") String id){

          try {
             EduTeacher teacher = eduTeacherService.getById(id);

               int a =10/0;
             if (teacher!=null){
            return R.ok().data("info",teacher);
              }else {
                   return R.error().message("没有找到");
              }
         }catch (Exception e){

              throw new GuliException(20001,"分母不可以为零");

          }


        }


}

